package com.thekernel.youdaoyun.service;

import com.thekernel.youdaoyun.YoudaoyunApplication;
import com.thekernel.youdaoyun.bean.Note;
import com.thekernel.youdaoyun.exception.MyException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * @Author TheKernel
 * @Date 2019/9/14 1:30 下午
 * @Version 1.0
 * @Description
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = YoudaoyunApplication.class)
public class testNoteService {

    @Autowired
    private NoteService noteService;

    @Test
    public void testAddNote() throws MyException {
        Note note = new Note();
        note.setNoteUser(1);
        note.setCategory(1);
        note.setNoteTitle("test.txt");
        note.setNoteContent("test\nonline");
        Assert.assertEquals(1, this.noteService.addNote(note, "test\ntest"));
    }

    @Test
    public void testQueryNote() throws MyException {
        String note = this.noteService.queryNote(3);
        System.out.println(note);
    }

    @Test
    public void testRecoverDeleteNote() throws MyException {
        long noteId = 1;  // 测试笔记 id

        int result = this.noteService.recoverDeletedNote(noteId);
        Assert.assertEquals(1, result);

    }

    public void testQueryNoteByNoteId() {
//        long noteId = 1;
//        Note note = this.noteService.
    }

    @Test
    public void testUpdateNote() throws MyException {
        HttpSession session = ((ServletRequestAttributes)
                Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
                .getRequest()
                .getSession();
        session.setAttribute("userId", 1L);
        long noteId = 6;
        String note = this.noteService.queryNote(noteId);
//        int result = this.noteService.updateNote(note, "test Update Note Service");
//        Assert.assertEquals(1, result);
    }

    @Test
    public void testRenameNote() throws MyException {
        HttpSession session = ((ServletRequestAttributes)
                Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
                .getRequest()
                .getSession();
        session.setAttribute("userId", 1L);
        Note note = new Note();
        note.setNoteId(3);
        note.setCategory(1);
        note.setNoteTitle("my.txt");
        this.noteService.renameNote(note);
    }

}
